<template>
    <div class="div1">
        <div class="div2">修改密码</div>
        <div style="width: 500px;margin: 0 auto;padding-top: 25px" class="div3">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="原密码" prop="oldPass">
                    <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off" show-password></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>


    </div>


</template>

<script>
import { updatePassword } from "@/api/user";

export default {
    name: "UpdatePassword",
    data() {
        var checkOldPass = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('原密码不能为空'));
            }
            callback()
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入新密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                oldPass: '',
                pass: '',
                checkPass: '',
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                oldPass: [
                    { validator: checkOldPass, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                  //alert('submit!');
                  let form = this.ruleForm
                  updatePassword(form.oldPass,form.pass).then((response) => {
                    if (response.msg === 'success') {
                      alert("修改成功，请重新登陆")
                      this.$store.dispatch('LogOut').then(() => {
                        this.$message({
                          type: 'success',
                          message: '退出成功!'
                        })
                        this.$router.push("/login")
                      })
                    }
                  })
                } else {
                    // console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped>
    .div1{
        width: 100%;
        height: 100%;
        margin: 0 auto;
    }

    .div2 {
        text-align: center;
        font-size: 18px;
        color: #333;
        line-height: 40px;
    }

</style>
